package com.bwie.service.impl;

import com.alibaba.fastjson.JSON;
import com.bwie.utils.R;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.mapper.CouponMapper;
import com.bwie.domain.Coupon;
import com.bwie.service.CouponService;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class CouponServiceImpl extends ServiceImpl<CouponMapper, Coupon> implements CouponService{

    @Autowired
    private CouponMapper couponMapper;

    @Autowired
    private StringRedisTemplate redisTemplate;
    @Override
    public R getCouponList(Coupon coupon) {
        List<Coupon> couponList = couponMapper.getCouponList(coupon);
        this.redisTemplate.opsForValue().set("coupon:", JSON.toJSONString(couponList));
        return R.OK(couponList);
    }

    @Override
    public R status01(List<Coupon> couponList) {
        for (Coupon coupon : couponList) {
            coupon.setActivityStatus(1);
            couponMapper.updateById(coupon);
        }
        return R.OK();
    }

    @Override
    public R status02(List<Coupon> couponList) {
        for (Coupon coupon : couponList) {
            coupon.setActivityStatus(2);
            couponMapper.updateById(coupon);
        }
        return R.OK();
    }

    @Override
    public R status03(List<Coupon> couponList) {
        for (Coupon coupon : couponList) {
            coupon.setActivityStatus(3);
            couponMapper.updateById(coupon);
        }
        return R.OK();
    }
}
